<template>
  <el-config-provider :locale="locale">
    <AppProvider>
      <router-view />
    </AppProvider>
  </el-config-provider>
</template>

<script lang="ts" setup>
  import { onErrorCaptured, provide } from 'vue';
  import { setLoclastorage, getLocalStorage } from '@/utils/tools';
  import AppProvider from '@/components/AppProvider/index.vue';
  import { useRoute, useRouter } from 'vue-router';
  import zhCn from 'element-plus/es/locale/lang/zh-cn';
  import en from 'element-plus/es/locale/lang/en';
  import { ILocaleMap, LocaleType } from './locales';

  const route = useRoute();
  const router = useRouter();
  // 刷新
  const reload = () => {
    console.log('App');
    router.replace({
      path: '/redirect' + route.fullPath,
    });
  };
  provide('reload', reload); // 向子组件提供reload方法

  // 设置语言
  const localeMap: ILocaleMap = {
    'zh-cn': zhCn,
    en,
  };
  let locale = zhCn;
  if (getLocalStorage('locale')) {
    locale = localeMap[getLocalStorage('locale') as LocaleType];
  }
  const setLanguage = (val: string) => {
    console.log(val);
    setLoclastorage('locale', val);
    window.location.reload();
  };
  provide('setLanguage', setLanguage); // 向子组件提供setLanguage方法

  // 错误边界
  onErrorCaptured((err, instance, info) => {
    console.error('[errorCaptured]', err, instance, info);
    return false;
  });
</script>
